package com.pinyoumei.app.activity.integral;

import android.content.ClipData;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.BaseActivityCurrency;
import com.pinyoumei.app.activity.ProductDetailActivity;
import com.pinyoumei.app.activity.ProductDetailPtActivity;
import com.pinyoumei.app.activity.wallet.CenterWalletBean;
import com.pinyoumei.app.adapter.BottomFirstAdapter;
import com.pinyoumei.app.adapter.IntegralRightAdapter;
import com.pinyoumei.app.bean.GoodDetailsBean;
import com.pinyoumei.app.bean.GoodsTypeBean;
import com.pinyoumei.app.bean.IntegralDetailsBean;
import com.pinyoumei.app.bean.ScoreIndexGoodsBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.pinyoumei.app.utils.Utils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IntegralShopActivity extends BaseActivityCurrency implements View.OnClickListener {

    private ImageView integralShopBack;
    private EditText integralShopEdit;
    private ListView integralShopLeft;
    private SmartRefreshLayout swipeRefreshLayout;
    private RecyclerView integralShopRecycl;
    private IntegralLeftMenuAdapter integralLeftMenuAdapter;
    private IntegralRightAdapter integralRightAdapter;
    private String menuId = "";
    private List<GoodsTypeBean.GoodsTypeDto> goodsMenuList;
    private int page = 1;
    private String queryName = "";
    private List<ScoreIndexGoodsBean.ScoreIndexGoodsDto> goodsList = new ArrayList<>();
    private TextView integralShopEditSearch;

    @Override
    protected int layoutId() {
        return R.layout.integral_shop_view;
    }

    @Override
    protected void initView() {
        integralShopBack = findViewById(R.id.integral_shop_back);
        integralShopEdit = findViewById(R.id.integral_shop_edit);
        integralShopLeft = findViewById(R.id.integral_shop_left);
        swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        integralShopRecycl = findViewById(R.id.integral_shop_recycl);
        integralShopEditSearch = findViewById(R.id.integral_shop_edit_search);
        integralShopBack.setOnClickListener(this);
        integralShopEditSearch.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        leftMenuData();
        scoreIndexTypeList(SPUtils.getAppUserId(IntegralShopActivity.this));
        rightListData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.integral_shop_back:
                finish();
                break;
            case R.id.integral_shop_edit_search:
                queryName = integralShopEdit.getText().toString();
                scoreIndexGoodsList(menuId);
                break;
        }
    }

    private void leftMenuData(){
        integralLeftMenuAdapter = new IntegralLeftMenuAdapter(IntegralShopActivity.this);
        integralShopLeft.setAdapter(integralLeftMenuAdapter);
        integralLeftMenuAdapter.notifyDataSetChanged();
        integralShopLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                menuId = goodsMenuList.get(position).getId();
                page = 1;
                for (int i = 0; i < goodsMenuList.size(); i++) {
                    goodsMenuList.get(i).setSelect(false);
                }
                goodsMenuList.get(position).setSelect(true);
                integralLeftMenuAdapter.notifyDataSetChanged();
                scoreIndexGoodsList(menuId);
            }
        });
    }


    private void scoreIndexTypeList(String user_id){
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",user_id);
        OkGo.<String>post(Constants.scoreIndexTypeList)
                .params("user_id", user_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDailog.dismiss();
                        GoodsTypeBean bean = gson.fromJson(response.body(), GoodsTypeBean.class);
                        goodsMenuList = bean.getResult().getGoods_type_list();
                        for (int i = 0; i <goodsMenuList.size() ; i++) {
                            if(i == 0){
                                goodsMenuList.get(i).setSelect(true);
                            }else{
                                goodsMenuList.get(i).setSelect(false);
                            }
                        }
                        integralLeftMenuAdapter.setData(goodsMenuList);
                        integralLeftMenuAdapter.notifyDataSetChanged();
                        scoreIndexGoodsList("");
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });
    }

    private void rightListData(){
        integralShopRecycl.setLayoutManager(new GridLayoutManager(IntegralShopActivity.this, 2, GridLayoutManager.VERTICAL, false));
        integralRightAdapter = new IntegralRightAdapter(R.layout.integral_right_list_item, null);
        integralRightAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        integralShopRecycl.setAdapter(integralRightAdapter);

        integralRightAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                InitGoodDate(integralRightAdapter.getData().get(position).getId());
            }
        });


        swipeRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                scoreIndexGoodsList(menuId);
                refreshLayout.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                queryName = integralShopEdit.getText().toString();
                scoreIndexGoodsList(menuId);
                refreshLayout.finishRefresh();
            }
        });
    }

    private void scoreIndexGoodsList(String mId){
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",SPUtils.getAppUserId(IntegralShopActivity.this));
        map.put("goods_type_id",mId);
        map.put("query_name",queryName);
        map.put("page",page);
        map.put("limit",10);
        OkGo.<String>post(Constants.scoreIndexGoodsList)
                .params("user_id", SPUtils.getAppUserId(IntegralShopActivity.this))
                .params("goods_type_id",mId)
                .params("query_name",queryName)
                .params("page",page)
                .params("limit",10)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDailog.dismiss();
                        ScoreIndexGoodsBean bean = gson.fromJson(response.body(), ScoreIndexGoodsBean.class);
                        if (bean.getCode() >= 0) {
                            if(page == 1){
                                goodsList.clear();
                            }
                            goodsList.addAll(bean.getResult().getGoods_list());
                            integralRightAdapter.setNewData(goodsList);
                        } else {
                            ToastUtils.showShort(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });
    }

    private void InitGoodDate(String goods_id) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(IntegralShopActivity.this));
        map.put("goods_id", goods_id);

        OkGo.<String>post(Constants.scoreIndexGoodsDetail)
                .params("user_id", SPUtils.getAppUserId(IntegralShopActivity.this))
                .params("goods_id", goods_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        IntegralDetailsBean integralDetailsBean = gson.fromJson(response.body(), IntegralDetailsBean.class);

                        if (integralDetailsBean.getCode() >= 0) {
                            Intent intent = new Intent(IntegralShopActivity.this, IntegralDetailActivity.class);
                            intent.putExtra("integralDetailsBean", integralDetailsBean);
                            intent.putExtra("goods_id", goods_id);
                            startActivity(intent);
                        } else {
                            Toast.makeText(IntegralShopActivity.this, integralDetailsBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }

                });
    }
}
